package com.china08.yxyapi.api.home.weikecheng;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.yxweike.YxyWeikeService;
import com.china08.yxyapi.util.YxyResUtil;

/**
 * 
 * 微课前台--列表页---按知识点,学段，排序筛选微课
 * 
 */
@CrossOrigin
@RestController
public class WeikeLstController extends YxyApiBaseController {

    @Autowired
    private YxyWeikeService weikeService;

    @ResponseBody
    @RequestMapping(value = "/api_yw/home/weike/lst")
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        reqModel.check();

        // 排序
        Pageable pageable = null;
        if (StringUtils.equals(reqModel.sortField, "1")) {
            pageable = reqModel.getPageable(Direction.fromString(reqModel.sortType), "uploadTime");
        }
        if (StringUtils.equals(reqModel.sortField, "2")) {
            pageable = reqModel.getPageable(Direction.fromString(reqModel.sortType), "mark");
        }
        if (StringUtils.equals(reqModel.sortField, "3")) {
            pageable = reqModel.getPageable(Direction.fromString(reqModel.sortType), "pv");
        }

        Page<ElasticYxyResResource> page = null;
        // 区域微课
        if (StringUtils.equals(reqModel.weikeType, "1")) {
            page = weikeService.findResourceEdu(reqModel.stage, reqModel.subject, reqModel.knowId, reqModel.areaId, "",
                    reqModel.falg, reqModel.bookId, reqModel.contentId, pageable);
        }
        // 本校微課--(已登录状态)
        if (StringUtils.equals(reqModel.weikeType, "2")) {
            String schoolId = reqModel.schoolId;
            Assert.notNull(StringUtils.defaultIfBlank(schoolId, null), "学校id不能为空");
            page = weikeService.findResourceSch(reqModel.stage, reqModel.subject, reqModel.knowId, schoolId, "",
                    reqModel.falg, reqModel.bookId, reqModel.contentId, pageable);
        }
        // 云校微課
        if (StringUtils.equals(reqModel.weikeType, "3")) {
            page = weikeService.findResourceYx(reqModel.stage, reqModel.subject, reqModel.knowId, "", reqModel.falg,
                    reqModel.bookId, reqModel.contentId, reqModel.flag, reqModel.type, pageable);
        }
        List<RespModel> list = new ArrayList<RespModel>();
        long total = 0l;
        if (page != null) {
            total = page.getTotalElements();
            for (ElasticYxyResResource resResource : page) {
                RespModel respModel = new RespModel();
                respModel.weikeId = resResource.getId();
                respModel.cover = resResource.getCover();
                respModel.title = resResource.getTitle();
                respModel.uploaderName = resResource.getUploaderName();
                respModel.pv = resResource.getPv();
                respModel.duration = YxyResUtil.getDurationString(resResource);
                respModel.isVip = resResource.getIsVip();
                respModel.isHighQualityYx = resResource.getIsHighQualityYx();
                list.add(respModel);
            }
        }
        Page<RespModel> pageImpl = new PageImpl<RespModel>(list, pageable, total);
        return pageImpl;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public String sortField = "1";// 最新上传时间1,评分2,浏览最多3

        public String sortType = "desc";

        public String stage;// 学段

        public String subject;// 科目

        public String knowId;// 知识点id

        public String schoolId;// 当前用户的学校id

        public String weikeType;// 微课范围类型：区域微课1,本校微课2,云校微课3
        // 区域Id码
        public String areaId;

        // 章节还是目录 true 是章节 false 是知识点
        public boolean falg;
        // 目录id
        public String contentId;
        // 默认第一次使用
        public String bookId;

        // 0 不限 1 vip 2 免费
        public Integer flag = 0;

        // 0 精品资源 1 普通资源 2不限
        public Integer type = 2;

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(sortField, null), "排序字段不能为空");
            Assert.notNull(StringUtils.defaultIfBlank(sortType, null), "排序类型不能为空");
            Assert.notNull(StringUtils.defaultIfBlank(weikeType, null), "微课共享范围不能为空");
        }

    }

    protected static class RespModel extends YxyApiRespModel {

        public String weikeId;// 微课Id

        public String cover;// 封面

        public String duration;// 时长

        public String title;// 标题

        public String uploaderName;// 上传者

        public Long pv;// 浏览量

        public Boolean isVip = false;

        public Integer isHighQualityYx = 0;
    }

}
